$(function () {
    let id = sessionStorage.getItem("add-teamId")
    getMemeber(id);
});

function getMemeber(id) {
    let result = myAjax('/member/findByGroupId', 'post', {id: id});
    let html = "";
    for (let i = 0; i < result.length; i++) {
        html += '   <tr> ' +
            '        <td>' + result[i].userId + '</td>\n' +
            '        <td>' + result[i].userName + '</td>\n' +
            '        <td>' + result[i].groupName + '</td>\n' +
            '        <td><input type="button" style="width: 60px;border-radius: 5px;' +
            'background-color: #5FB878" value="已到"' +
            'onclick="arrived(' + result[i].userId + ',`' + result[i].userName + '`,`' + result[i].groupName + '`)">' +
            '         <input type="button" style="width: 60px;border-radius: 5px;' +
            'background-color: red" value="迟到" ' +
            'onclick="late(' + result[i].userId + ',`' + result[i].userName + '`,`' + result[i].groupName + '`)">' +
            '         <input type="button" style="width: 60px;border-radius: 5px;' +
            'background-color: darksalmon" value="请假" ' +
            'onclick="vacate(' + result[i].userId + ',`' + result[i].userName + '`,`' + result[i].groupName + '`)"></td>' +
            '    </tr>';
    }
    $("#findAll").html(html);
}

layui.use("layer");

function arrived(userId, userName, groupName) {
    let data = {
        userId: userId,
        userName: userName,
        team: groupName,
        state: "正常",
        signDate: findDate(),
        signTime: findTime(),
    }
    let result = newAddSign(data);
    if (result === 1) {
        layer.msg("点到成功")
    } else if (result===2) {
        layer.msg("该成员今日已经点过到啦！！")
    } else {
        layer.msg("服务器开小差了,请重新点到哦！")
    }
}

function late(userId, userName, groupName) {
    let data = {
        userId: userId,
        userName: userName,
        team: groupName,
        state: "迟到",
        signDate: findDate(),
        signTime: findTime(),
    }
    let result = newAddSign(data);
    if (result === 1) {
        layer.msg("记录成功")
    } else if (result===2) {
        layer.msg("该成员今日已经点过到啦！！")
    } else {
        layer.msg("服务器开小差了,请重新记录哦！")
    }
}

function vacate(userId, userName, groupName) {
    let data = {
        userId: userId,
        userName: userName,
        team: groupName,
        state: "请假",
        signDate: findDate(),
        signTime: findTime(),
    }
    let result = newAddSign(data);
    if (result === 1) {
        layer.msg("记录成功")
    } else if (result===2) {
        layer.msg("该成员今日已经点过到啦！！")
    } else {
        layer.msg("服务器开小差了,请重新记录哦！")
    }
}

function newAddSign(data) {
    let result = myAjax('/sign/add', 'post', data);
    return result;
}

function findDate() {
    let dateTime = new Date();
    let year = dateTime.getFullYear();
    let month = dateTime.getMonth() + 1;
    let ri = dateTime.getDate();
    return (year + "-" + month + "-" + ri);
}

function findTime() {
    let dateTime = new Date();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    return (hours + ":" + minutes + ":" + seconds)
}